<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use App\Models\Squad;
use App\Models\Crouse;
use App\Models\Classroom;
use App\Models\Notice;


class Credits extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'credits';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //每月2号生成学分表
        $year = date('Y',strtotime('-1 month'));
        $month = date('m',strtotime('-1 month'));

        $start_time = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month')));
        $end_time = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')));

        $squads = DB::table('squads as s')->join('class_section_groups as csg','csg.id','=','s.section')->get(['s.id','csg.name']);

        $rules = DB::table('credits_rules')->orderBy('section','asc')->get();
        $rule = json_decode(json_encode($rules),true);
        foreach($squads as $v){
            //对每个班进行操作
            $squad_data = array();
            $squad_data['squad_id'] = $v->id;
            $squad_data['year'] = intval($year);
            $squad_data['month'] = intval($month);
            $squad_data['create_time'] = date('Y-m-d H:i:s');
            $id = DB::table('credits_squads')->insertGetId($squad_data);

            //找到时间段内所有日程
            $notice = DB::table('notices')->whereIn('status',['2','4'])->where('squad_id',$v->id)
            ->whereRaw('UNIX_TIMESTAMP(`starttime`) >= '.$start_time)
            ->whereRaw('UNIX_TIMESTAMP(`starttime`) <= '.$end_time)->get(['id','notice_type']);

            $kq_notice = $test_notice = $jx_notice = array();
            //统计有多少次家校、测试、考勤
            $kq_num = $test_num = $jx_num = 0;

            foreach($notice as $_v){
                if($_v->notice_type == '1'){
                    //考勤
                    $kq_notice[] = $_v->id;
                    $kq_num++;
                }else if($_v->notice_type == '3'){
                    //家校
                    $jx_notice[] = $_v->id;
                    $jx_num++;
                }else if($_v->notice_type == '2'){
                    //测试
                    $test_notice[] = $_v->id;
                    $test_num++;
                }
            }

            $student = array();
            //获取每种类型
            $kq = DB::table('attendance_records')->whereIn('notice_id',$kq_notice)->get(['student_id','attendance_status']);
            foreach($kq as $_v){
                if(!isset($student[$_v->student_id]['kaoqin'])){
                    $student[$_v->student_id]['kaoqin'] = array();
                }
                if(!isset($student[$_v->student_id]['test'])){
                    $student[$_v->student_id]['test'] = array();
                }
                if(!isset($student[$_v->student_id]['jiaxiao'])){
                    $student[$_v->student_id]['jiaxiao'] = array();
                }
                $student[$_v->student_id]['kaoqin'][] = $_v->attendance_status;
            }

            $test = DB::table('test_score_records')->whereIn('notice_id',$test_notice)->get(['student_id','test_status','test_score']);
            $test_rule = DB::table('notices as n')->join('test_assigns as ta','ta.id','=','n.test_assign_id')
                ->join('test_configs as tc','tc.id','=','ta.test_config_id')->whereIn('n.id',$test_notice)->select('n.id','tc.total_score')->get();
            $test_arr = array();
            foreach($test_rule as $_va){
                $test_arr[$_va->id] = $_va->total_score;
            }

            foreach($test as $_v){
                if(!isset($student[$_v->student_id]['kaoqin'])){
                    $student[$_v->student_id]['kaoqin'] = array();
                }
                if(!isset($student[$_v->student_id]['test'])){
                    $student[$_v->student_id]['test'] = array();
                }
                if(!isset($student[$_v->student_id]['jiaxiao'])){
                    $student[$_v->student_id]['jiaxiao'] = array();
                }
                $student[$_v->student_id]['test'][] = array('status'=>$_v->test_status,'score'=>$_v->test_score / $test_arr[$_v->notice_id] * 100);
            }

            $jx = DB::table('jiaxiao_records')->whereIn('notice_id',$jx_notice)->get(['student_id','jiaxiao_status','jiaxiao_score','notice_id']);
            foreach($jx as $_v){
                if(!isset($student[$_v->student_id]['kaoqin'])){
                    $student[$_v->student_id]['kaoqin'] = array();
                }
                if(!isset($student[$_v->student_id]['test'])){
                    $student[$_v->student_id]['test'] = array();
                }
                if(!isset($student[$_v->student_id]['jiaxiao'])){
                    $student[$_v->student_id]['jiaxiao'] = array();
                }
                $student[$_v->student_id]['jiaxiao'][] = array('status'=>$_v->jiaxiao_status,'score'=>$_v->jiaxiao_score);
            }

            //计算学分
            $insert = array();
            $num_arr = array('kq'=>$kq_num,'test'=>$test_num,'jx'=>$jx_num);
            if($v->name == '一段班'){
                $insert = $this->calculate_credits($student,$rule[0],$id,$num_arr);
            }elseif($v->name == '二段班'){
                $insert = $this->calculate_credits($student,$rule[1],$id,$num_arr);
            }
            DB::table('credits')->insert($insert);
        }
    }

    //计算学分
    //所有学分都可以概括为一个公式
    //                 总计*比例
    //Σ(每一项得分) *  ———————————
    //                 次数*满分
    public function calculate_credits($student,$rule,$pid,$num)
    {
        $return = array();

        foreach($student as $k => $v){
            $temp = $content = array();
            $temp['student_id'] = $k;
            $temp['pid'] = $pid;

            //考勤
            $i=1;$json = array();$count = 0;
            foreach($v['kaoqin'] as $_v){
                if($_v == '1'){
                    $count += $rule['kq_zc'];
                    $json['考勤-'.$i] = '正常';
                }elseif($_v == '2'){
                    $count += $rule['kq_qj'];
                    $json['考勤-'.$i] = '请假';
                }elseif($_v == '3'){
                    $count += $rule['kq_cd'];
                    $json['考勤-'.$i] = '迟到';
                }elseif($_v == '4'){
                    $count += $rule['kq_qq'];
                    $json['考勤-'.$i] = '缺勤';
                }
                $i++;
            }
            $content['kaoqin'] = $json;
            if($num['kq'] == 0){
                $temp['kq_credits'] = 0;
            }else{
                $temp['kq_credits'] = $count * $rule['total_score']*($rule['kq_zb']/100)/($num['kq']*$rule['kq_total']);
            }
            $temp['kq_credits'] = round($temp['kq_credits'],10);


            //作业
/*            $i=1;$json = array();$jcount = $fcount = 0;
            foreach($v['zuoye'] as $_v){
                $fcount += $_v['score'];
                if($_v['status'] == '1'){
                    $jcount += $rule['zy_zj'];
                    $json['作业-'.$i] = '准交-'.$_v['score'];
                }elseif($_v == '2'){
                    $jcount += $rule['zy_wj'];
                    $json['作业-'.$i] = '未交-'.$_v['score'];
                }elseif($_v == '3'){
                    $jcount += $rule['zy_cj'];
                    $json['作业-'.$i] = '补交-'.$_v['score'];
                }
                $i++;
            }
            $content['zuoye'] = $json;
            //准交的学分
            $jcredits = $jcount * $rule['total_score']*($rule['zy_zj_zb']/100)/($num['zy']*$rule['zy_total']);
            //分数的学分
            $fcredits = $fcount * $rule['total_score']*($rule['zy_df_zb']/100)/($num['zy']*$rule['zy_df_mf']);
            $temp['zy_credits'] = round($jcredits+$fcredits,10);*/

            //测试
            $i=1;$json = array();$jcount = $fcount = 0;
            foreach($v['test'] as $_v){
                if($_v['status'] == '1'){
                    $jcount += 1;
                    $fcount += $_v['score'];
                    $json['测试-'.$i] = '参加-'.$_v['score'];
                }elseif($_v == '2'){
                    $json['测试-'.$i] = '未参加';
                }
                $i++;
            }
            $content['test'] = $json;

            if($num['test'] == 0){
                $jcredits = 0;
                $fcredits = 0;
            }else{
                //准交的学分
                $jcredits = $jcount * $rule['total_score'] * $rule['cs_zj_zb'] / ($num['test']*100);
                //分数的学分
                $fcredits = $fcount * $rule['total_score']*($rule['cs_mf_zb']/100)/($num['test']*100);
            }
            $temp['test_credits'] = round($jcredits+$fcredits,10);

            //家校
            $i=1;$json = array();$jcount = $fcount = 0;
            foreach($v['jiaxiao'] as $_v){
                $fcount += $_v['score'];
                if($_v['status'] == '1'){
                    $jcount += $rule['jx_zj'];
                    $json['家校-'.$i] = '准交-'.$_v['score'];
                }elseif($_v == '2'){
                    $jcount += $rule['jx_wj'];
                    $json['家校-'.$i] = '未交-'.$_v['score'];
                }elseif($_v == '3'){
                    $jcount += $rule['jx_cj'];
                    $json['家校-'.$i] = '补交-'.$_v['score'];
                }
                $i++;
            }
            $content['jiaxiao'] = $json;

            if($num['jx'] == 0){
                $jcredits = 0;
                $fcredits = 0;
            }else{
                //准交的学分
                $jcredits = $jcount * $rule['total_score']*($rule['jx_zj_zb']/100)/($num['jx']*$rule['jx_total']);
                //分数的学分
                $fcredits = $fcount * $rule['total_score']*($rule['jx_df_zb']/100)/($num['jx']*$rule['jx_df_mf']);
            }
            $temp['jx_credits'] = round($jcredits+$fcredits,10);


            $temp['content'] = json_encode($content);
            $temp['credits'] = $temp['kq_credits'] + $temp['jx_credits'] + $temp['test_credits'];

            $return[] = $temp;
        }

        return $return;
    }
}
